
make_event_study_LAPOP = function(model){
  
  m = model 
  
  table_data = cbind.data.frame(
    est = m$coeftable[,1], 
    ci.h = m$coeftable[,1]+m$coeftable[,2]*1.96,
    ci.l = m$coeftable[,1]-m$coeftable[,2]*1.96, 
    id = row.names(m$coeftable), 
    id_keep = substring(row.names(m$coeftable), 1,5) 
  ) %>% 
    filter(id_keep == 'trend') %>% 
    mutate(trend_time = as.numeric(gsub('trend::', '', id)), 
           post = as.numeric(trend_time>=0, 1,0)
    )
  
  
  plot = ggplot(table_data, aes(trend_time, est, shape = factor(post))) + 
    geom_point(size = 2) + 
    geom_hline(yintercept = 0) + 
    geom_segment(aes(x=0,xend=max(trend_time),
                     y=mean(est[trend_time>=0]),
                     yend=mean(est[trend_time>=0]), 
                     linetype='Post'
    ), col = 'blue') + 
    geom_segment(aes(x=min(trend_time),xend=0,
                     y=mean(est[trend_time<0]),
                     yend=mean(est[trend_time<0]),
                     linetype='Pre'
    ), 
    col = 'blue', 
    ) + 
    geom_ribbon(aes(ymin=ci.l, ymax=ci.h), alpha=0.2) + 
    xlab('Days Relative to Verdict') + 
    ylab('Difference from Baseline') + 
    theme_minimal() + 
    scale_shape_discrete(labels=c('Pre', 'Post'),  name = 'Verdict Timing') + 
    theme(legend.position = 'bottom') + 
    scale_linetype_discrete(name = 'Average Difference')
  
  return(plot)
  
}


m1 = feols(torture_bin ~ i(trend, ref = c(-24:-18)) + q1 + q2 | factor(estratopri) + factor(estratosec) + weekend, data = lapop12, cluster = lapop12$idnum)
m2 = feols(soc.cl_bin ~ i(trend, ref = c(-24:-18)) + q1 + q2 | factor(estratopri) + factor(estratosec) + weekend, data = lapop12, cluster = lapop12$idnum)
m3 = feols(fair_trial ~ i(trend, ref = c(-24:-18)) + q1 + q2 | factor(estratopri) + factor(estratosec) + weekend, data = lapop12, cluster = lapop12$idnum)

torture_plot = make_event_study_LAPOP(m1) 
torture_plot + geom_vline(xintercept = -3) + 
  geom_vline(xintercept = -10) + 
  annotate("label", x = -9, y = Inf, 
           label = 'Lead up to Remembrance Day', 
           hjust=0, vjust=1)
ggsave('fig-out/torture_plot.pdf')
ggsave('fig-out/torture_plot.png', width = 5411, height = 2051, units = "px", dpi=600)


soc_cl_plot = make_event_study_LAPOP(m2) 
soc_cl_plot = soc_cl_plot+ geom_vline(xintercept = -3) + 
  geom_vline(xintercept = -10) + 
  annotate("label", x = -9, y = Inf, 
           label = 'Lead up to Remembrance Day', 
           hjust=0, vjust=1)
ggsave('fig-out/soc_cl_plot.pdf')
ggsave('fig-out/soc_cl_plot.png',  width = 5411, height = 2051, units = "px", dpi=600)


fair_plot = make_event_study_LAPOP(m3) 
fair_plot = fair_plot+ 
  annotate("label", x = -9, y = Inf, 
           label = 'Lead up to Remembrance Day', 
           hjust=0, vjust=1)
ggsave('fig-out/fair_plot.pdf')
ggsave('fig-out/fair_plot.png',  width = 5411, height = 2051, units = "px", dpi=600)

